package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.StoreDao;
import com.delivery.www.po.Store;
import com.delivery.www.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class StoreDaoImpl implements StoreDao {
    @Override
    public int createStore(String storeName, int userId, int dishNum, int commentNum, String introduce, int processOrderNum, int unprocessOrderNum, Float turnover, int status) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {storeName, userId, dishNum, commentNum, introduce, processOrderNum, unprocessOrderNum, turnover, status};
        String sql = "insert into store (store_name,user_id,dish_num,comment_num,introduce,process_order_num,unprocess_order_num,turnover,status) values(?,?,?,?,?,?,?,?,?)";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public Store getOwnStore(int userId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId};
        String sql = "select store_id,store_name,user_id,introduce,dish_num,comment_num,process_order_num,unprocess_order_num,turnover,status from store where user_id = ?";
        Store store = null;
        if (connection != null) {
            store = BaseDao.getForOne(connection, Store.class, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return store;
    }

    @Override
    public List<Store> getAllStore(String storeName, int currentPageNo, int pageSize) {
        Connection connection = JdbcUtils.getConnection();
        List<Store> stores = null;
        if (connection != null) {
            StringBuilder sql = new StringBuilder();
            sql.append("select s.store_id,store_name,user_id,introduce,dish_num,comment_num,process_order_num,unprocess_order_num,turnover,status from store s where status = 1");
            List<Object> list = new ArrayList<>();
            if (!StringUtils.isNullOrEmpty(storeName)) {
                sql.append(" and store_name like ?");
                list.add("%" + storeName + "%");
            }
            sql.append(" order by process_order_num DESC limit ?,?");
            currentPageNo = (currentPageNo - 1) * pageSize;
            list.add(currentPageNo);
            list.add(pageSize);
            Object[] params = list.toArray();
            stores = BaseDao.getList(connection, Store.class, sql.toString(), params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return stores;
    }

    @Override
    public int updateStoreName(int storeId, String value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, storeId};
        String sql = "update store set store_name = ? where store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public int updateStoreIntroduce(int storeId, String value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, storeId};
        String sql = "update store set introduce = ? where store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public List<Store> getAuditStore() {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select s.store_id,store_name,user_id,introduce,dish_num,comment_num,process_order_num,unprocess_order_num,turnover,status from store s where status = 0";
        List<Store> stores = null;
        if (connection != null) {
            stores = BaseDao.getList(connection, Store.class, sql);
        }
        JdbcUtils.closeResource(connection, null, null);
        return stores;
    }

    @Override
    public List<Store> getStore() {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select s.store_id,store_name,user_id,introduce,dish_num,comment_num,process_order_num,unprocess_order_num,turnover,status from store s ";
        List<Store> stores = null;
        if (connection != null) {
            stores = BaseDao.getList(connection, Store.class, sql);
        }
        JdbcUtils.closeResource(connection, null, null);
        return stores;
    }

    @Override
    public int modifyStatus(int storeId, int status) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {status, storeId};
        String sql = "update store set status = ? where store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public int getStoreCount(String storeName) {
        Connection connection = JdbcUtils.getConnection();
        int count = 0;
        if (connection != null) {
            StringBuilder sql = new StringBuilder();
            sql.append("select count(1) count from store");
            List<Object> list = new ArrayList<>();
            if (!"".equals(storeName)) {
                sql.append(" where store_name like ?");
                list.add("%" + storeName + "%");
            }
            Object[] params = list.toArray();
            count = BaseDao.selectCount(connection, sql.toString(), params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return count;
    }
}
